//
//  PersonCell.m
//  Hxcsfw
//
//  Created by qiysoft on 2018/1/29.
//  Copyright © 2018年 企友软mac. All rights reserved.
//

#import "PersonCell.h"

@implementation PersonCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.BView1.layer.masksToBounds = YES;
    self.BView1.layer.cornerRadius = 5;
    self.BView1.layer.borderWidth = 1;
    self.BView1.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.BView2.layer.masksToBounds = YES;
    self.BView2.layer.cornerRadius = 5;
    self.BView2.layer.borderWidth = 1;
    self.BView2.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.NextBtn.layer.masksToBounds = YES;
    self.NextBtn.layer.cornerRadius = 5;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

- (IBAction)NextBtn:(id)sender {//下一步按钮
    
    self.NextBtn.enabled = NO;
    if(![self panduanShouji] ) {
        return;
    }
    if(![self checkInput] ) {
        return;
    }

    [self requestVerifyCode:self.PhoneTF.text];

//    [_delegate XiaYiBu:sender];
    
}
- (IBAction)YesOrNoBtn:(id)sender {
    [_delegate XieYi:sender];
}

- (IBAction)agreementBtn:(id)sender {
    [_delegate DaKaiXieYi:sender];
}

#pragma mark - 获取验证码功能
- (void)requestVerifyCode:(NSString *)phone {
    //验证码注册时参数
    //    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"regist",@"content":@{@"revert":@"0",@"dto":@{@"phone":self.PhoneTF.text,@"code":self.TPYzmTF.text}}};

    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"shortlogin",@"content":@{@"revert":@"getcode",@"phone":phone}};

    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};
    
    
    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];
        
        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];
        
        if ([returnDic[@"status"] isEqualToString:@"发送成功！"]) {
            //召唤主线程改变UI
            [self performSelectorOnMainThread:@selector(fsyzmcg) withObject:nil waitUntilDone:YES];
        }else{
            [SVProgressHUD showImage:nil status:@"发送失败！"];
            [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
            [SVProgressHUD dismissWithDelay:1.];
            [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];
        }
        
        self.NextBtn.enabled = YES;

    } failure:^(NSError *error) {
        [SVProgressHUD showImage:nil status:@"发送失败！"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
        [SVProgressHUD dismissWithDelay:1.];
        [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];
        
        self.NextBtn.enabled = YES;

    } showProgress:YES];
}

//判断手机号
-(BOOL)panduanShouji {
    BOOL resultBool = NO;
    
    if (![self.PhoneTF.text isUserPhone]) {
        [WShareUtil showAlert:@"温馨提示" message:@"请输入正确的手机号！"];
        self.NextBtn.enabled = YES;
        return resultBool;
    }
    return YES;
}

#pragma mark - 判断填空条件
-(BOOL)checkInput{
    BOOL resultBool = NO;
    
    //----------------------是否同意注册协议
    if (self.YesOrNoBtn.selected) {//没同意协议
        [WShareUtil showAlert:@"温馨提示" message:@"未阅读并同意注册协议！"];
        self.NextBtn.enabled = YES;
        return resultBool;
    }
    
    NSString *userNameString = self.PhoneTF.text;
    if ([WShareUtil strNilOrEmpty:userNameString]) {
        [WShareUtil showAlert:@"温馨提示" message:@"手机号不能为空！"];
        self.NextBtn.enabled = YES;

        return resultBool;
    }
    
    userNameString = [userNameString stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:userNameString]) {
        [WShareUtil showAlert:@"温馨提示" message:@"手机号不能全部为空格！"];
        self.NextBtn.enabled = YES;
        
        return resultBool;
    }
    
    if ([WShareUtil strNilOrEmpty:self.TPYzmTF.text]) {
        [WShareUtil showAlert:@"温馨提示" message:@"图片验证码不能为空！"];
        self.NextBtn.enabled = YES;
        
        return resultBool;
    }
    
    NSString *tpyzmtf = self.TPYzmTF.text;
    tpyzmtf = [tpyzmtf stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:tpyzmtf]) {
        [WShareUtil showAlert:@"温馨提示" message:@"图片验证码不能全部为空格！"];
        self.NextBtn.enabled = YES;
        
        return resultBool;
    }
    
    if (![[self.TPYzmTF.text lowercaseString] isEqualToString:[WShareUtil sharedData].RegistSmallTpyzm]) {
        [WShareUtil showAlert:@"温馨提示" message:@"图片验证码错误！请重新输入"];
        self.NextBtn.enabled = YES;
        
        return resultBool;
    }
    
    return YES;
}

-(void)fsyzmcg {
    [_delegate XiaYiBu:_NextBtn];
}

@end
